package com.rumor.background.Controller;


import com.rumor.background.Mapper.FoundMapper;
import com.rumor.background.Utils.Contribute.ContributeUtils;
import com.rumor.background.Utils.Utils;
import com.rumor.background.pojo.Found;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

// 失物招领（找到）
@RestController
public class FoundController {

    @Autowired
    FoundMapper mapper;

    @GetMapping("/getFoundCode")
    public String getLostCode(){
        String found_code = Utils.getFoundCode();
        Found fd = mapper.queryFoundByFoundCode(found_code);
        while(fd != null){
            found_code = Utils.getFoundCode();
            fd = mapper.queryFoundByFoundCode(found_code);
        }
        return found_code;
    }

    @PostMapping("/foundImgUpload")
    public int rumorImgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
        File directory = new File("FOUND");
        String username = request.getParameter("username");
        String found_code = request.getParameter("found_code");
        String filename = request.getParameter("filename");
        Found fd = mapper.queryFoundByFoundCode(found_code);

        // 如果数据库中存在该随机编码，直接将图片保存到对应位置
        if(fd != null){
            // 如果open_id不一样，可能出了问题，投稿失败
            if(!username.equals(fd.getUsername())) return -1;
            // 保存图片
            String path = directory.getCanonicalPath() + "/" +  fd.getImg_location();
            Utils.saveImgFile(request, path, filename);
            return 1;
        }

        // 如果不存在该随机编码对应的投稿，创建图片保存路径并保存图片到本地，数据库中记录投稿信息
        else{
            String date = ContributeUtils.getDate();
            String img_location = date + "/" + found_code + "/";
            fd = new Found(username, found_code, null, img_location, date, "-1");
            String path = directory.getCanonicalPath() + "/" +  img_location;
            Utils.saveImgFile(request, path, filename);
            return mapper.addFound(fd);
        }
    }

    @PostMapping("/deleteFoundImg")
    public int deleteRumorImg(@RequestParam("username") String username, @RequestParam("found_code") String found_code,
                              @RequestParam("filename") String filename) throws IOException {
        Found rc = mapper.queryFoundByFoundCode(found_code);
        if(!rc.getUsername().equals(username)) return -1;
        File directory = new File("FOUND");
        String path = directory.getCanonicalPath() + "/" + rc.getImg_location();
        return Utils.deleteImgFile(path, filename + ".jpg");
    }

    @PostMapping("/foundSubmit")
    public int rumorSubmit(@RequestParam("username") String username, @RequestParam("found_code") String found_code,
                           @RequestParam(value = "remark", required = false) String remark){
        Found fd = mapper.queryFoundByFoundCode(found_code);
        // 如果投稿不存在或open_id不一样，可能出了问题，投稿失败
        if(fd == null || !username.equals(fd.getUsername())) return -1;
        remark = (remark == null || remark.length() == 0)?"此投稿无备注":remark;
        fd.setRemark(remark);
        fd.setMark("0");
        return mapper.updateFound(fd);
    }

}
